package com.ltb.speedlottery;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.ltb.speedlottery.adapter.CommonAdapter;
import com.ltb.speedlottery.adapter.ViewHolder;
import com.ltb.speedlottery.bean.Data;
import com.ltb.speedlottery.bean.GoodLuckLotteryData;
import com.ltb.speedlottery.utils.Computer;
import com.qmuiteam.qmui.widget.QMUIAnimationListView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 红运彩
 */
public class GoodLuckLotteryActivity extends AppCompatActivity implements Response.Listener<GoodLuckLotteryData.Data>, Response.ErrorListener{
    private static final String URL = "https://app.51hyc.vip/api/lottery/lists?page=1&type=sfssc";
    private static final String ANALYSIS_DATA = "234567";
    private static final String REFRESH_URL = "";
    private SwipeRefreshLayout mRefreshLayout;
    private QMUIAnimationListView mListView;
    private TextView mAnalysisCountTV;
    private CommonAdapter<GoodLuckLotteryData.ItemInfo> mAdapter;
    private List<GoodLuckLotteryData.ItemInfo> mDatas = new ArrayList<>();
    private Gson mGson = new Gson();
    private int mAnalysisCount=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_luck_lottery);
        init();
        queryData();
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {

    }

    @Override
    public void onResponse(final GoodLuckLotteryData.Data data) {
        if(data != null && data.lists != null && data.lists.size()>0){
            mListView.manipulate(new QMUIAnimationListView.Manipulator<CommonAdapter<Data>>() {
                @Override
                public void manipulate(CommonAdapter adapter) {
                    mDatas.clear();
                    mDatas.addAll(data.lists);
                    mAdapter.notifyDataSetChanged();
                }
            });
            mRefreshLayout.setRefreshing(false);
            mAnalysisCountTV.setText(
                    String.format(getString(R.string.analysis_count),
                            ANALYSIS_DATA,
                            String.valueOf(mAnalysisCount)));
        }
    }

    private void init() {
        mAnalysisCountTV = (TextView)findViewById(R.id.tv_analysis_count);
        mListView = (QMUIAnimationListView)findViewById(R.id.lv_gool_luck_lottery_history);
        mAdapter = new CommonAdapter<GoodLuckLotteryData.ItemInfo>(this, mDatas, R.layout.item_good_luck_lottery_history_layout) {
            @Override
            public void convert(ViewHolder helper, GoodLuckLotteryData.ItemInfo item, int position) {
                helper.setText(R.id.tv_number, String.format(getString(R.string.item_lottery_code), item.expect));
                if("已开奖".equals(item.status) && item.open != null){
                    GoodLuckLotteryData.OpenCode openCode = mGson.fromJson(item.open.toString(), GoodLuckLotteryData.OpenCode.class);
                    helper.setText(R.id.tv_data_first, openCode.dwd.get(0));
                    helper.setText(R.id.tv_data_second, openCode.dwd.get(1));
                    helper.setText(R.id.tv_data_3thd, openCode.dwd.get(2));
                    helper.setText(R.id.tv_data_4thd, openCode.dwd.get(3));
                    helper.setText(R.id.tv_data_5thd, openCode.dwd.get(4));
                    helper.getView(R.id.tv_code_status).setVisibility(View.GONE);
                    helper.getView(R.id.layout_data).setVisibility(View.VISIBLE);
                    boolean isWin = Computer.analysisItemData(item.code, ANALYSIS_DATA);
                    helper.getView(R.id.tv_data_win).setVisibility(isWin ? View.VISIBLE : View.GONE);
                } else {
                    helper.getView(R.id.tv_code_status).setVisibility(View.VISIBLE);
                    helper.getView(R.id.layout_data).setVisibility(View.GONE);
                    helper.setText(R.id.tv_code_status, item.status);
                }
//                helper.setText(R.id.tv_lottery_code, formatLotteryCode(item.code));
            }
        };
        mListView.setAdapter(mAdapter);
        mRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh_layout);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryData();
            }
        });
    }

    private void queryData(){
        new MyTask().execute();
    }

    private class MyTask extends AsyncTask<String, Void, GoodLuckLotteryData.Data>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mRefreshLayout.setRefreshing(true);
        }

        @Override
        protected GoodLuckLotteryData.Data doInBackground(String... strings) {
            Document doc = null;
            try {
                doc = Jsoup.connect(URL).ignoreContentType(true).get();
                Log.d("Tianbing", "doInBackground====> "+ doc.body().text());
            } catch (IOException e) {
                Log.d("Tianbing", "doInBackground catch====> "+  e.toString());
            }
            if(doc != null){
                GoodLuckLotteryData lotteryRoot = mGson.fromJson(doc.body().text(), GoodLuckLotteryData.class);
                if(lotteryRoot != null && "success".equals(lotteryRoot.msg)){
                    mAnalysisCount = Computer.analysisAllData(lotteryRoot.data.lists, ANALYSIS_DATA);
                    return lotteryRoot.data;
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(GoodLuckLotteryData.Data data) {
            onResponse(data);
        }
    }
}
